<?php

namespace App\Listeners\Like;

use App\Consts\GlobalConst;
use App\Consts\GlobalCounterConst;
use App\Consts\GlobalMessageConst;
use App\Events\Like\CancelLikeEvent;
use App\Listeners\BaseListener;

class CancelLikeListener extends BaseListener
{
    /**
     * Handle the event.
     *
     * @param CancelLikeEvent $event
     * @return bool
     */
    public function handle(CancelLikeEvent $event)
    {
        $this->saveFileLog(__CLASS__, $event);

        $msgLikeReceive = $event->msgLikeReceive;

        // 更新被点赞用户的未读消息数
        service()->MessageService->changeUnreadCount([
            'user_id' => $msgLikeReceive['receive_id'],
            'message_type' => GlobalMessageConst::COUNT_TYPE_LIKE,
            'count' => GlobalCounterConst::DECREASE
        ]);

        // 队列记录日志
        service()->LogCountService->createLogByQueue($event->logCountLoadData);

        // 【暂时】如果赞的是评论，要修改评论的<hot>
        if ($msgLikeReceive['target_type'] == GlobalConst::TARGET_TYPE_COMMENT) {
            service()->CommentService->changeHotByQueue([
                'comment_id' => $msgLikeReceive['target_id'],
                'trend' => $event->logCountLoadData['action_type'] == GlobalCounterConst::ACTION_TYPE_LIKE ?
                    GlobalCounterConst::INCREASE : GlobalCounterConst::DECREASE
            ]);
        }

        return true;
    }
}
